#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTextEdit>
#include <QFileDialog>
#include <ostream>
#include <filesystem>
#include <fstream>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    /*
    QMenuBar *menubar = new QMenuBar();
    this->setMenuBar(menubar);

    QMenu *menu1 = new QMenu("file");
    QMenu *menu2 = new QMenu("Edit");
    QMenu *menu3 = new QMenu("View");
    QMenu *menu4 = new QMenu("Build");

    menubar->addMenu(menu1);
    menubar->addMenu(menu2);
    menubar->addMenu(menu3);
    menubar->addMenu(menu4);

    QAction *action1 = new QAction("new");
    QAction *action2 = new QAction("save");
    QAction *action3 = new QAction("save as");
    QAction *action4 = new QAction("exit");
    menu1->addAction(action1);
    // 添加分割线
    menu1->addSeparator();
    menu1->addAction(action2);
    // menu1->addSeparator();
    menu1->addAction(action3);
    // menu1->addSeparator();
    menu1->addAction(action4);
    // menu1->addSeparator();

    connect(action1, &QAction::triggered, this, [this](){handle("action1");});
    connect(action4, &QAction::triggered, this, &MainWindow::close);
    */
    this->setWindowTitle("My notebook");
    QMenuBar *menubar = new QMenuBar(this);
    this->setMenuBar(menubar);

    QMenu *menu1 = new QMenu("File");
    menubar->addMenu(menu1);

    QAction *action1 = new QAction("save");
    QAction *action2 = new QAction("load");
    menu1->addAction(action1);
    menu1->addSeparator();
    menu1->addAction(action2);

    QTextEdit *edit = new QTextEdit(this);
    this->setCentralWidget(edit);
    edit->setPlaceholderText("edit the text context here");

    connect(action1, &QAction::triggered, this, [this, edit](){
        QFileDialog *dialog = new QFileDialog(this);
        QString filename = dialog->getSaveFileName(this, "保存文件", "C:\\Users\\lenovo1\\Desktop");
        qDebug() << "filename: " << filename;
        std::ofstream file(filename.toStdString().c_str());
        if (!file.is_open())
        {
            qDebug() << "file save failed";
        }
        const QString &text = edit->toPlainText();
        file << text.toStdString();
        file.close();
    });
    connect(action2, &QAction::triggered, this, [this, edit](){
        QFileDialog *dialog = new QFileDialog(this);
        QString filename = dialog->getOpenFileName(this, "加载文件", "C:\\Users\\lenovo1\\Desktop");
        qDebug() << "filename: " << filename;

        std::ifstream file(filename.toStdString().c_str());
        if (!file.is_open())
        {
            qDebug("file load failed");
            return;
        }
        std::string context;
        std::string line;
        while (std::getline(file, line))
        {
            context += line;
            context += "\n";
        }
        file.close();

        QString text = QString::fromStdString(context);
        edit->setPlainText(text);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handle(const QString res)
{
    qDebug() << res + "按钮被点击";
}
